//
//  cinemaViewController.m
//  movie
//
//  Created by 李丹阳 on 15/7/22.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "cinemaViewController.h"
#import "dataService.h"
#import "cinemaViewCell.h"

@interface cinemaViewController (){
    BOOL flags[100];
}


@end

@implementation cinemaViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    _cinemaDataArray = [[NSMutableArray alloc]init];
    _groupArray = [[NSMutableArray alloc]init];
    

    [self _loadData];
    [self _createTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_createTableView{
    _cinemaTableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _cinemaTableView.delegate = self;
    _cinemaTableView.dataSource = self;
    
    [self.view addSubview:_cinemaTableView];
    
    UINib* nib = [UINib nibWithNibName:@"cinemaViewCell" bundle:nil];
    [_cinemaTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    _cinemaTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
}

- (void)_loadData{
//    _cinemaDataArray = [[NSMutableArray alloc]init];
//    _groupArray = [[NSMutableArray alloc]init];
//    
    NSDictionary* cinemaDic = [dataService getJSONDataFromFile:@"cinema_list"];
    NSArray* cinemaArray = [cinemaDic objectForKey:@"cinemaList"];
    for (NSDictionary* dic in cinemaArray) {
        cinemaModal* modal = [[cinemaModal alloc]init];
        modal.name = [dic objectForKey:@"name"];
        modal.grade = [dic objectForKey:@"grade"];
        modal.address = [dic objectForKey:@"address"];
        modal.lowPrice = [dic objectForKey:@"lowPrice"];
        modal.isCouponSupport = [dic objectForKey:@"isCouponSupport"];
        modal.isSeatSupport = [dic objectForKey:@"isSeatSupport"];
        modal.districtId = [dic objectForKey:@"districtId"];
        [_cinemaDataArray addObject:modal];
    }

    
    NSDictionary* districtDic = [dataService getJSONDataFromFile:@"district_list"];
    NSArray* districtArray = [districtDic objectForKey:@"districtList"];

    for (NSDictionary* dic in districtArray) {

        groupModal* gModal = [[groupModal alloc]init];
        gModal.cinemaModal = [[NSMutableArray alloc]init];
        gModal.groupId = [dic objectForKey:@"id"];
        gModal.name = [dic objectForKey:@"name"];
        
        for (cinemaModal* modal in _cinemaDataArray) {
            if ([modal.districtId isEqualToString: gModal.groupId]) {
                [gModal.cinemaModal addObject:modal];
//                NSLog(@"%@",modal);
            }
        }
        
//        NSLog(@"%@",gModal);
        [_groupArray addObject:gModal];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _groupArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cinemaViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    groupModal* group =  _groupArray[indexPath.section];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.modal = group.cinemaModal[indexPath.row];
    cell.backgroundColor = [UIColor blackColor];
    

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    groupModal* group =  _groupArray[section];
    
    if (flags[section] == NO ) {
        return 0;
    }
    return group.cinemaModal.count;
}

//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton* button = [[UIButton alloc]init];
    [button setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal"] forState:UIControlStateNormal];
    groupModal* group =  _groupArray[section];
    
    [button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:group.name forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tag = section;
//    button.titleLabel.textAlignment = NSTextAlignmentRight;
    return button;
    
}
//
- (void)ButtonAction:(UIButton*)btn{
    NSInteger index = btn.tag;
    flags[index] = !flags[index];

    NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:index];
    [_cinemaTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
    

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
